feat(ascend): add prefix-shared attention Ascend C kernel - #340
Open
zhangj1an wants to merge 2 commits into
Open
feat(ascend): add prefix-shared attention Ascend C kernel#340zhangj1an wants to merge 2 commits into
zhangj1an wants to merge 2 commits into
Conversation
- csrc/ascend/attention/prefix_shared_attention_ascend.asc: Ascend C prefix-shared fused attention forward (bf16, fp32 online softmax over fixed 64-key tiles, one block per (bs, g, 64-row query block), no split-K -> batch-invariant); D=128, non-causal, same surface as the CUDA prefix_shared_attention op - rl_engine/kernels/ops/ascend/attention/prefix_shared_attn.py: PrefixSharedAttentionAscendOp wrapper (op(q, k, v) -> out) - gtest: prefix_shared_attention spec (pytorch/cuda/ascend candidates) with GtestPrefixSharedAttentionOp gold; check_operator.py learns --device npu - pybind consolidated in csrc/ascend/ops_npu.asc (single PYBIND11_MODULE; batch_invariant_logp_ascend.asc only drops its module block) - tests/test_prefix_shared_attention_ascend.py: correctness, batch invariance, validation - setup.py: recursive .asc glob
zhangj1an
requested review from
Flink-ddd,
KJLdefeated,
bitborne and
inaniloquentee
as code owners
August 25, 2026 09:45
|
Important Review skippedAuto reviews are disabled on base/target branches other than the default branch. Please check the settings in the CodeRabbit UI or the ⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Plus Run ID: You can disable this status message by setting the Use the checkbox below for a quick retry:
Comment |
…scend-prefix-shared-attention # Conflicts: # rl_engine/kernels/gtest/operator_inputs.py # rl_engine/kernels/gtest/operator_specs.py # setup.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Latest Status [25 Aug 2026]
Will fix CI error then mark as ready for review
Summary
Port of the CUDA prefix-shared fused attention (
csrc/cuda/attention/prefix_shared_attention.cu, the GRPO decode workload) to an Ascend NPU version:Forward is implemented as an Ascend C (CANN) kernel,
_C_npu.prefix_shared_attention_ascend:q [bs, G, len_q, 128],k/v [bs, len_kv, 128]— the G generated responses share one prompt-prefix KV sequence, stored once per batch instead of once per group (same surface as the CUDAPrefixSharedAttentionOp: bf16 only, D=128, non-causal, no key-padding mask, forward-only).(bs, g, 64-row query block)is processed end-to-end by one AI-core block, streaming the keys in a fixed 64-key tile order with fp32 online-softmax accumulation (per-row max / sum-exp rescaling per tile — the same flash-style single pass as the CUDA kernel).len_kv— outputs are batch-invariant: bitwise identical regardless of batch size, batch position, or how many blocks were launched (items are strided across blocks).Integration:
prefix_shared_attention(op classattention, reusing its tolerance contract) withpytorch/cuda/ascendcandidates and aGtestPrefixSharedAttentionOpgold that expands the shared K/V over G and reusesNativeAttentionOp.forward_fp32(non-causal, default scale).scripts/check_operator.pylearns--device npu(defensivetorch_npuprobe) so the ascend candidates run in the gtest harness on NPU hosts.setup.py.ascglob changed to recursive**/*.asc, allowing kernels to follow the CUDA-style directory structure undercsrc/ascend/attention/.Build notes (same pattern as PR #320)
Each
.ascsource file can define only onePYBIND11_MODULE, because linking multiple sources with Bisheng causes a duplicatePyInit__C_npuerror. Therefore pybind registrations are consolidated incsrc/ascend/ops_npu.asc(logp + prefix-shared attention), while individual kernel files contain only the kernel and host forward functions.Files
csrc/ascend/attention/prefix_shared_attention_ascend.asccsrc/ascend/ops_npu.asc_C_npupybind registration file (logp + prefix-shared attention). New.csrc/ascend/batch_invariant_logp_ascend.ascPYBIND11_MODULE, which is moved to the aggregated registration file. Kernel logic is unchanged.rl_engine/kernels/ops/ascend/attention/prefix_shared_attn.pyPrefixSharedAttentionAscendOpwrapper, same surface as the CUDAPrefixSharedAttentionOp(op(q, k, v) -> out) with bf16/D=128 validation. New.rl_engine/kernels/ops/ascend/attention/__init__.pyrl_engine/kernels/gtest/operator_specs.pyprefix_shared_attentionspec (pytorch/cuda/ascend candidates) and theGtestPrefixSharedAttentionOpgold.rl_engine/kernels/gtest/operator_inputs.pyscripts/check_operator.py--device npusupport (defensivetorch_npuprobe).tests/test_prefix_shared_attention_ascend.pytests/test_ws1_gtest_gpu.pyprefix_shared_attentionto the registered-ops coverage set.setup.py.ascglob to**/*.asc.Test
Build first (same as PR #320):
gtest
pytest
Test results
Environment: 8× Ascend 910, CANN 8.5.1 (Bisheng), torch 2.7.1 + torch_npu 2.7.1.
suite=prefix_shared_attention passed=True pass_rate=1.0000suite=prefix_shared_attention passed=True pass_rate=1.0000suite=batch_invariant_logp passed=True pass_rate=1.0000tests/test_prefix_shared_attention_ascend.pytests/test_batch_invariant_logp.py(regression)gtest: prefix_shared_attention ascend bf16 (raw)
gtest: prefix_shared_attention pytorch bf16 (raw)
gtest: batch_invariant_logp ascend bf16 regression (raw)
pytest: tests/test_prefix_shared_attention_ascend.py (raw)
pytest: tests/test_batch_invariant_logp.py regression (summary)
(all
TestAscend*classes passed; skips are CUDA/Triton-only tests on this NPU host)Notes
PrefixSharedAttentionOp.PrefixSharedAttentionOpis consumed directly (e.g.benchmarks/benchmark_attention.py) and is not registered either; the Ascend op follows the same surface.ruff checkpasses for all modified Python files.